package com.gxuwz.leaveSystem.controller;

import com.gxuwz.leaveSystem.Util.ExcelExporter;
import com.gxuwz.leaveSystem.entity.Leave;

import com.gxuwz.leaveSystem.service.LeaveService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
public class LeaveController {

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    LeaveService leaveService = (LeaveService) applicationContext.getBean("leaveService");

    @GetMapping("/export")
    public ResponseEntity<byte[]> exportLeaves() throws IOException {
        List<Leave> leaves = leaveService.getAllLeaves(); // 假设你有一个方法获取所有请假记录

        byte[] excelBytes = ExcelExporter.exportLeavesToExcel(leaves);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/vnd.ms-excel"));
        headers.setContentDispositionFormData("attachment", "leaves.xlsx");
        headers.setContentLength(excelBytes.length);

        return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
    }

    /**
     * 清单列表
     * @return 全部清单记录和视图
     */
    @RequestMapping("/lv_list")
    public ModelAndView listLeaves() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/leave/leave_list");
        try {
            List<Leave> leaves = leaveService.getAllLeaves();
            mv.addObject("leave",leaves);
            return mv;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("查找列表异常");
        }
        return mv;
    }

    /**
     * 添加列表
     * @param leave
     * @return 视图
     */
    @RequestMapping("/leave_add")
    public ModelAndView add(Leave leave){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/lv_list");
        try {
            int num = leaveService.addLeave(leave);
            if (num == 1){
                System.out.println("添加成功");
                return mv;
            }else {
                System.out.println("添加失败");
                return mv;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("添加异常");
        }
        return mv;
    }

    /**
     * 生成3位随机数字,从 0 到 899 之间随机选择一个整数，然后再加上 100
     * 定向到添加视图
     * @return 预定的数据
     */
    @RequestMapping("/lv_addView")
    public ModelAndView addView(){
        ModelAndView mv = new ModelAndView();
        Random random = new Random();
        int randomNum = random.nextInt(900) + 100;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmsss");
        String timestamp = sdf.format(new Date());
        String leaveID = timestamp+randomNum;

        Leave leave = new Leave(leaveID,"1","生病了",3,"202209512228",null,"0",null,"批准");
        mv.addObject("leave",leave);
        mv.setViewName("/leave/leave_add");
        return mv;
    }

    /**
     * 删除清单
     * @param id
     * @return
     */
    @RequestMapping("/lv_delete")
    public ModelAndView delete(String id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/lv_list");
        try {
            int num = leaveService.deleteLeave(id);
            if (num == 1){
                System.out.println("删除成功");
                return mv;
            }else {
                System.out.println("删除失败");
                return mv;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("删除异常");
        }
        return mv;
    }

    /**
     * 修改清单
     * @param leave
     * @return 列表
     */
    @RequestMapping("/lv_edit")
    public ModelAndView update(Leave leave){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/lv_list");
        try {
            int num = leaveService.updateLeave(leave);
            if (num==1){
                System.out.println("更新成功");
                return mv;
            }else {
                System.out.println("更新失败");
                return mv;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("更新异常");
        }

        return mv;
    }

    /**
     * 转到编辑页面
     * @param leaveID
     * @return 列表视图
     */
    @RequestMapping("/lv_editView")
    public ModelAndView editView(@RequestParam("leaveID") String leaveID){
        System.out.println(leaveID);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/leave/leave_edit");
        try {
                Leave leave = leaveService.getLeaveById(leaveID);
                mv.addObject("leave",leave);
                return mv;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("查找异常");
        }
        return mv;
    }

    /**
     * 根据关键字搜索
     * @param keyword
     * @return 列表
     */
    @RequestMapping("/lv_search")
    public ModelAndView search(String keyword) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/leave/leave_list");
        try {
            List<Leave> leaves = leaveService.leavesSearch(keyword);
            if (keyword!=null&&leaves!=null){
                mv.addObject("leave",leaves);
                return mv;
            }else {
                System.out.println("查找不到或没有！");
                return mv;
            }
        }catch (Exception e){
            System.out.println("搜索异常");
        }
        return mv;
    }

}
